"""
Case Type   : datakit安装
Case Name   : 【实例管理】查询实例
Create At   : 2025/5/20
Owner       : @lonely-dance
Description :
    1.添加实例
    2.输入存在的集群名称查询实例
    3.输入存在的ip查询实例
    4.输入存在的类型查询实例
    5.集群名称、IP地址、类型皆为空查询实例
    6.输入不存在的集群名称查询实例
    7.输入不存在的ip查询实例
    8.输入不存在的类型查询实例
    3.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
    5.成功
    6.成功
    7.成功
    8.成功
History     :
"""

import os
import re
import unittest

from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.datakit_api.CommonApi import CommonApi
from testcase.utils.CommonDatakit import CommonDatakit


class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.com_sh = CommonSH()
        self.pri_node = Node('PrimaryDbUser')
        self.mysql_node = Node('mysql5')
        self.datakit_node = Node('Datakit_Root')
        self.token = self.com_sh.get_token_value(macro.DATAKIT_PASSWD)
        self.log.info(self.token)
        self.assertNotEqual("error", self.token)
        self.com_api = CommonApi(self.token)
        self.com_datakit = CommonDatakit(self.token)
        self.cluster_name = re.search(r'Case\d+', os.path.basename(__file__)).group()
        self.url = "jdbc:opengauss://{self.pri_node.db_host}:{self.pri_node.db_port}/{self.pri_node.db_name}"

    def test_base_ops(self):
        text = '----step1:添加实例 expect：成功----'
        self.log.info(text)
        self.com_datakit.import_db([self.pri_node])

        text = '----step2:输入存在的集群名称查询实例 expect:成功----'
        self.log.info(text)
        cluster_name = f'{self.pri_node.ssh_host}({self.pri_node.db_port})-1'
        response = self.com_api.get_jdbcDbCluster_page(name=cluster_name)
        self.log.info(response.json())
        self.assertEqual(1, response.json()['total'], '执行失败' + text)

        text = '----step3:输入存在的ip查询实例 expect:成功----'
        self.log.info(text)
        cluster_ip = f'{self.pri_node.ssh_host}'
        response = self.com_api.get_jdbcDbCluster_page(ip=cluster_ip)
        self.log.info(response.json())
        self.assertEqual(1, response.json()['total'], '执行失败' + text)

        text = '----step4:输入存在的类型查询实例 expect:成功----'
        self.log.info(text)
        db_type = 'OPENGAUSS'
        response = self.com_api.get_jdbcDbCluster_page(db_type=db_type)
        self.log.info(response.json())
        self.assertEqual(1, response.json()['total'], '执行失败' + text)

        text = '----step5:输入不存在的集群名称查询实例 expect:成功----'
        self.log.info(text)
        cluster_name = f'aaa'
        response = self.com_api.get_jdbcDbCluster_page(name=cluster_name)
        self.log.info(response.json())
        self.assertEqual(0, response.json()['total'], '执行失败' + text)

        text = '----step6:输入不存在的ip查询实例 expect:成功----'
        self.log.info(text)
        cluster_ip = f'10.10.10.10'
        response = self.com_api.get_jdbcDbCluster_page(ip=cluster_ip)
        self.log.info(response.json())
        self.assertEqual(0, response.json()['total'], '执行失败' + text)

        text = '----step7:输入不存在的类型查询实例 expect:成功----'
        self.log.info(text)
        db_type = 'MYSQL'
        response = self.com_api.get_jdbcDbCluster_page(db_type=db_type)
        self.log.info(response.json())
        self.assertEqual(0, response.json()['total'], '执行失败' + text)

    def tearDown(self):
        self.log.info('----step8:清理环境 expect:成功----')
        self.com_datakit.delete_db()
        self.log.info(f"-----{os.path.basename(__file__)} finsh-----")
